package com.xinjian.jianba.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xinjian.jianba.dto.GroupDetailVo;
import com.xinjian.jianba.dto.GroupIdDto;
import com.xinjian.jianba.dto.GroupMembersVo;
import com.xinjian.jianba.dto.GroupsVo;
import com.xinjian.jianba.dto.JoinGroupDto;
import com.xinjian.jianba.dto.QuitGroupDto;
import com.xinjian.jianba.dto.UserTrackBuziNamesDto;
import com.xinjian.jianba.dto.UserTrackBuziNamesUsersVo;
import com.xinjian.jianba.dto.UserTrackBuziNamesVo;
import com.xinjian.jianba.dto.UserTrackBuziUsersDto;
import com.xinjian.jianba.exception.BusinessException;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.RongYunGroupService;
import com.xinjian.jianba.service.ValidatorService;
import com.xinjian.jianba.utils.JsonUtil;

import io.rong.models.Result;

@RestController
@RequestMapping("/api/v3/jianba/business")
public class RongYunGroupControllor extends AbstractController{
	private static final Logger log = LoggerFactory.getLogger(RongYunGroupControllor.class);
	
	
	@Autowired
	private RongYunGroupService rongYunGroupService;
	@Autowired
	private ValidatorService validatorService;
	/**
	 * 用户加入指定群组
	 */
	@PostMapping("/joinGroup")
	public String joinGroup(@RequestBody JoinGroupDto joinGroupDto){
		log.info("用户加入指定群组------{}",JsonUtil.getJsonString(joinGroupDto));
		try {
			validatorService.valid(joinGroupDto);
		} catch (Exception e) {
			throw new BusinessException(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		rongYunGroupService.joinGroup(joinGroupDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
	}
	/**
	 * 邀请用户加入群组
	 */
	@PostMapping("/inviteGroup")
	public String inviteGroup(@RequestBody JoinGroupDto joinGroupDto){
		log.info("邀请用户加入群组------{}",JsonUtil.getJsonString(joinGroupDto));
		try {
			validatorService.valid(joinGroupDto);
		} catch (Exception e) {
			throw new BusinessException(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		rongYunGroupService.inviteGroup(joinGroupDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS,ResponseCode.SUCCESS_MSG);
	}
	/**
	 * 查询群成员方法
	 */
	@PostMapping("/getGroupInfos")
	public String getGroupInfos(@RequestBody GroupIdDto groupIdDto){
		log.info("查询群成员------{}",JsonUtil.getJsonString(groupIdDto));
		try {
			validatorService.valid(groupIdDto);
		} catch (Exception e) {
			throw new BusinessException(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		GroupMembersVo result=rongYunGroupService.getGroupInfos(groupIdDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS,ResponseCode.SUCCESS_MSG,result);
	}
	/**
	 * 退出群组
	 */
	@PostMapping("/quitGroup")
	public String quitGroup(@RequestBody QuitGroupDto quitGroupDto){
		log.info("退出群组------{}",JsonUtil.getJsonString(quitGroupDto));
		try {
			validatorService.valid(quitGroupDto);
		} catch (Exception e) {
			throw new BusinessException(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		Result ryResult=rongYunGroupService.quitGroup(quitGroupDto);
		return this.responseClientJsonData(ryResult.getCode(), ryResult.getMsg());
		
	}
	/**
	 * 查看用户所在群组
	 */
	@PostMapping("/groupsWithMe")
	public String groupsWithMe(@RequestBody Map<String, String> map){
		log.info("查看用户所在群组------{}",JsonUtil.getJsonString(map));
		String userId=map.get("userId");
		if(StringUtils.isBlank(userId)){
			throw new BusinessException(ResponseCode.BAD_REQUEST,"用户id不能为空");
		}
		List<GroupsVo> list=rongYunGroupService.groupsWithMe(userId);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG,list);
	}
	/**
	 * 根据群组id获取群组名称和头像
	 * @param map
	 * @return
	 */
	@PostMapping("/groupDetail")
	public String groupDetail(@RequestBody Map<String, String> map){
		log.info("groupDetail------{}",map);
		String groupId=map.get("groupId");
		if(StringUtils.isBlank(groupId)){
			throw new BusinessException(ResponseCode.BAD_REQUEST,"群组id不能为空");
		}
		GroupDetailVo groupDetailVo=rongYunGroupService.groupDetail(groupId);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG,groupDetailVo);
	}
	/**
	 * 获取用户去过的商家
	 */
	@PostMapping("/userTrackBuziNames")
	public String userTrackBuziNames(@RequestBody UserTrackBuziNamesDto userTrackBuziNamesDto){
		log.info("userTrackBuziNames------{}",userTrackBuziNamesDto);
		try {
			validatorService.valid(userTrackBuziNamesDto);
		} catch (Exception e) {
			throw new BusinessException(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		List<UserTrackBuziNamesVo> list=rongYunGroupService.trackBuziNames(userTrackBuziNamesDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG,list);
	}
	/**
	 * 查看去过该商家的所有人
	 */
	@PostMapping("/buziNameTrackUsers")
	public String buziNameTrackUsers(@RequestBody UserTrackBuziUsersDto userTrackBuziUsersDto){
		log.info("buziNameTrackUsers------{}",userTrackBuziUsersDto);
		try {
			validatorService.valid(userTrackBuziUsersDto);
		} catch (Exception e) {
			throw new BusinessException(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		List<UserTrackBuziNamesUsersVo> list=rongYunGroupService.buziNameTrackUsers(userTrackBuziUsersDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG,list);
	}
}
